﻿using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using Client.FlightsSearchService;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: {0} <flights search server URI>", AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            try
            {
                var uri = args[0];
                if (!uri.StartsWith("http://"))
                {
                    uri = "http://" + uri;
                }

                var cf = new WebChannelFactory<IFlightSearchService>(new WebHttpBinding(), new Uri(uri));
                IFlightSearchService channel = cf.CreateChannel();

                Console.WriteLine("Client is running");
                Console.WriteLine("Available commands: search, reserve, cancel, exit");

                var ce = new CommandExecuter(channel);
                ce.ExecuteUserCommands();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occured: {0}, exiting", e.Message);
            }
        }
    }
}
